package www.ym.com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.dto.OrdersDto;
import www.ym.com.entity.OrderDetail;
import www.ym.com.entity.Orders;
import www.ym.com.dao.OrdersCount;
import www.ym.com.mapper.OrdersMapper;
import www.ym.com.service.OrderDetailService;
import www.ym.com.service.OrdersService;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     *
     * @param ordersDto
     * @return
     */
    @PostMapping("/add")
    private R<String> submitOrder(@RequestBody OrdersDto ordersDto) {
        ordersService.submit(ordersDto);
        return R.success("", "提交订单成功");
    }

    /**
     * 修改订单状态
     *
     * @param orders
     * @return
     */
    @PostMapping("/status")
    private R<String> updateOrder(@RequestBody Orders orders) {
        String msg = ordersService.updateOrder(orders);
        return R.success("", msg);
    }

    /**
     * 查询不同状态下的订单
     *
     * @return
     */
    @PostMapping("/list")
    public RL<Orders> statusOrder(@RequestBody JSONObject jsonObject) {
        Page<Orders> userPage = ordersService.Pagelist(jsonObject);

        List<Orders> records = userPage.getRecords();
        long total = userPage.getTotal();
        long pageNum = userPage.getCurrent();
        long pageSize = userPage.getSize();
        return RL.listSuccess(records, null, total, pageNum, pageSize);
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getByID")
    public R<OrdersDto> getByIdOrder(Long id) {
        Orders orders = ordersService.getById(id);
        OrdersDto ordersDto = new OrdersDto();
        BeanUtils.copyProperties(orders, ordersDto);

        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderId, id);
        List<OrderDetail> list = orderDetailService.list(wrapper);
        ordersDto.setOrderDetails(list);

        return R.success(ordersDto, "处理成功");
    }

    /**
     * 每天的消费数据
     *
     * @param
     * @return
     */
    @PostMapping("/ym")
    public R<Object> YearMonthData(@RequestBody JSONObject jsonObject) {

        Long userId = Long.valueOf(String.valueOf(jsonObject.get("userId")));
        String orderTime = (String.valueOf(jsonObject.get("orderTime")));
        log.info("时间"+orderTime);

        List<OrdersCount> objects = ordersMapper.selectDayData(userId, orderTime);

        JSONArray dayList = new JSONArray();
        JSONArray countList = new JSONArray();
        JSONArray moneyList = new JSONArray();
        HashMap<Object, Object>  consumDayData = new HashMap<>();

        for (OrdersCount item:objects
             ) {
            dayList.add(item.getDay());
            countList.add(item.getCount());
            moneyList.add(item.getMoneyCount());

            consumDayData.put("day",dayList);
            consumDayData.put("count",countList);
            consumDayData.put("money",moneyList);
        }

        return R.success(consumDayData, "处理成功");
    }

    /**
     * 每天的消费数据
     *
     * @param
     * @return
     */
    @PostMapping("/monthData")
    public R<Object> monthData(@RequestBody JSONObject jsonObject) {

        Long userId = Long.valueOf(String.valueOf(jsonObject.get("userId")));
        String orderTime = (String.valueOf(jsonObject.get("orderMonth")));
        log.info("时间"+orderTime);

        List<OrdersCount> objects = ordersMapper.selectMonthData(userId, orderTime);

        JSONArray dayList = new JSONArray();
        JSONArray countList = new JSONArray();
        JSONArray moneyList = new JSONArray();

        HashMap<Object, Object>  consumDayData = new HashMap<>();
        for (OrdersCount item:objects
        ) {
            dayList.add(item.getDay());
            countList.add(item.getCount());
            moneyList.add(item.getMoneyCount());

            consumDayData.put("month",dayList);
            consumDayData.put("count",countList);
            consumDayData.put("money",moneyList);
        }

        return R.success(consumDayData, "处理成功");


    }


//
//    /**
//     * 用户下单
//     * @param orders
//     * @return
//     */
//    @PostMapping("/submit")
//    private R<String> submit(@RequestBody Orders orders){
//        ordersService.submit(orders);
//        return R.success("下单成功");
//    }
//
//    /**
//     * 订单页面显示
//     * @param page
//     * @param pageSize
//     * @return
//     */
//    @GetMapping("/page")
//    public R<Page> page(int page, int pageSize,  String number, String beginTime,String endTime,HttpSession session) {
//        //验证是否接受到信息
//        //log.info("page:{}", page);
//        //log.info("pageSize:{}", pageSize);
//        //页面构造器
//        Page<Orders> pageInfo = new Page<>(page, pageSize);
//        //查询所有订单
//        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//
//        //查询该用户的所有订单信息
//
//        queryWrapper.like(number!=null,Orders::getNumber,number);
//        queryWrapper.orderByAsc(Orders::getOrderTime);
//        queryWrapper.between(beginTime != null && endTime != null,Orders::getOrderTime,beginTime,endTime);
//        ordersService.page(pageInfo, queryWrapper);
//        return R.success(pageInfo);
//    }
//
//    /**
//     * 更改订单状态
//     * @param orders
//     * @return
//     */
//    @PutMapping
//    public R<Orders> status(@RequestBody Orders orders){
//        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Orders::getId,orders.getId());
//        ordersService.updateById(orders);
//        return R.success(orders);
//    }
//
//    /**
//     * 移动端订单列表
//     * @param page
//     * @param pageSize
//     * @return
//     */
//    @GetMapping("/userPage")
//    public R<Page> page(int page, int pageSize) {
//        //验证是否接受到信息
//        //页面构造器
//        Page<Orders> pageInfo = new Page<>(page, pageSize);
//        //查询所有订单
//        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
//        //查询该用户的所有订单信息
//        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
//        queryWrapper.orderByDesc(Orders::getOrderTime);
//        ordersService.page(pageInfo, queryWrapper);
//        return R.success(pageInfo);
//    }
}
